package com.ytzl.itrip.service.impl;

import com.ytzl.itrip.common.DigestUtil;
import com.ytzl.itrip.exception.UserLoginFailedException;
import com.ytzl.itrip.pojo.ItripUser;
import com.ytzl.itrip.service.ItripUserService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sam on 2018/2/7.
 */
@Service("itripUserService")
public class ItripUserServiceImpl extends BaseService implements ItripUserService {


    @Override
    public ItripUser findByUserName(String name) {
        Map<String, Object> param = new HashMap();
        param.put("userCode", name);
        List<ItripUser> itripUsers = null;
        try {
            itripUsers = itripUserMapper.getItripUserListByMap(param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (itripUsers != null && itripUsers.size() > 0)
            return itripUsers.get(0);
        return null;
    }

    @Override
    public void save(ItripUser itripUser) {
        //发送激活邮件
        //1.获取激活编号
        String activationCode = DigestUtil.digest(System.currentTimeMillis() + "");
        //2.发送
        mailService.sendActivationMail(itripUser.getUserCode(), activationCode);
        try {
            itripUserMapper.saveItripUser(itripUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean activate(String user, String code) {
        String key = PREFIX_ACTIVATION + user;
        if (redisAPI.exist(key)) {
            //获取激活码，进行比对
            if (redisAPI.get(key).equals(code)) {
                //比对通过，激活成功
                ItripUser itripUser = findByUserName(user);
                itripUser.setActivated(1);
                itripUser.setFlatID(itripUser.getId());
                try {
                    itripUserMapper.modifyItripUser(itripUser);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public void saveByPhone(ItripUser itripUser) {
        //发送短信
        //1.生成验证码
        //random() 生成[0,1)的数
        //(int)(Math.random()*8999)生成 [0-8999]的值
        // 所以合起来是[1000,9999]
        String code = ((int) (Math.random() * 9000) + 1000) + "";
        //发送短信
        smsService.sendActivationSms(itripUser.getUserCode(), "1", new String[]{code, "1"});
        //将短信code保存起来
        redisAPI.set(PREFIX_ACTIVATION + itripUser.getUserCode(), 60, code);
        try {
            itripUserMapper.saveItripUser(itripUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public ItripUser login(String name, String password) throws UserLoginFailedException {
        ItripUser itripUser = findByUserName(name);
        if (null != itripUser && itripUser.getUserPassword().equals(password)) {
            //用户未激活
            if (itripUser.getActivated() != 1) {
                throw new UserLoginFailedException("用户未激活");
            }
            return itripUser;
        }
        return null;
    }

//    @Override
//    public boolean activatePhone(String user, String code) {
//        String key = "activation:" + user;
//        if (redisAPI.exist(key)) {
//            if (redisAPI.get(key).equals(code)) {
//                ItripUser itripUser = findByUserName(user);
//                //值为1时 ，已激活
//                itripUser.setActivated(1);
//                itripUser.setFlatID(itripUser.getId());
//                itripUserMapper.modify(itripUser);
//                return true;
//            }
//        }
//        return false;
//    }

}
